// -----------------------------------------------------------------------
//  <copyright file="SoftDeleteQueryFilterConfiguration.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/24 21:11</last-date>
// -----------------------------------------------------------------------

namespace DaprPlus.EntityFrameworkCore;

/// <summary>
/// 软删除查询过滤器批量配置
/// </summary>
public class SoftDeleteQueryFilterConfiguration : IEntityBatchConfiguration
{
    public void Configure(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
    {
        var entityType = mutableEntityType.ClrType;
        if (typeof(ISoftDeletable).IsAssignableFrom(entityType))
        {
            var parameter = Expression.Parameter(entityType, "m");
            var body = Expression.Equal(
                Expression.Property(Expression.Convert(parameter, typeof(ISoftDeletable)), "DeletedTime"),
                Expression.Constant(EntityExtensions.UnDeletedLocalTime)
            );
            var lambda = Expression.Lambda(body, parameter);
            modelBuilder.Entity(entityType).HasQueryFilter(lambda);
        }
    }
}
